<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Spatie\Permission\Models\Role;

class RoleController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            $params = $request->all();

            $params = json_decode($params['params'], true);

            $where = $params['search'] ?? [];
            $limit = $params['pageSize'] ?? '10';
            $offset = ($params['pageCode'] - 1) * $limit ?? '1';

            //获取数据
            $data = Role::where($where)
                ->limit($limit)
                ->offset($offset)
                ->get();


            //循环给予每个角色一个权限数组
            foreach ($data as $key => $value) {
                //获取当前角色的所有允许权限
                $allowPermissions = $value->getAllPermissions()->toArray();

                $data[$key]['allPermissions'] = $allowPermissions;
            }

            //获取数据总数
            $total = Role::count();

            return $this->successReturn(200, [
                'total' => $total,
                'list' => $data
            ]);

        } catch (\Exception $e) {
            return $this->errorReturn(500, $e->getMessage());
        }


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $params = [
                'name' => $request->name,
                'description' => $request->description
            ];
            //配置验证
            $rules = [
                'name' => 'required',
            ];
            $message = [
                'name.required' => '[name]缺失',
            ];

            $this->verifyParams($params, $rules, $message);

            if (!Role::create($params)) $this->throwExp('添加用户失败');

            //正确返回信息
            return $this->successReturn(200, '添加用户成功');

        } catch (\Exception $e) {
            return $this->errorReturn('400', json_encode($e->getMessage()));

        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            if (!intval($id)) $this->throwExp('未知id参数');

            $params = [
                'name' => $request->name,
                'description' => $request->description
            ];
            //配置验证
            $rules = [
                'name' => 'required',
            ];
            $message = [
                'name.required' => '[name]缺失',
            ];

            $this->verifyParams($params, $rules, $message);

            if (!Role::where('id', $id)->update($params)) $this->throwExp('修改用户失败');

            //正确返回信息
            return $this->successReturn(200, '修改用户成功');

        } catch (\Exception $e) {
            return $this->errorReturn(500, $e->getMessage());
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (!intval($id)) return $this->errorReturn(500, '参数错误');

        if (!Role::destroy($id)) return $this->errorReturn(500, '删除失败');


        return $this->successReturn(200, '删除成功');

    }

    /**
     * @author YiYuan Lin
     * @date 18/12/11
     * @describe 获取所有角色列表
     * @return int
     */
    public function roleList()
    {
        $roleList = Role::select('id', 'name')
                ->orderBy('id' ,'asc')
                ->get()
                ->toArray();

         return $this->successReturn(200, $roleList);

    }
}
